package com.shengyin.ec.service.order;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.controller.account.ShowUserOrderParam;
import com.shengyin.ec.model.dao.order.OrderDao;
import com.shengyin.ec.model.entity.OrderItem;
import com.shengyin.ec.model.entity.Orders;

@Service(value ="orderService")
public class OrderServiceImpl implements OrderService{

	@Autowired
	OrderDao orderDao;
	
	
	public List<Orders> getOrderList(){
		List<Orders> orderList = orderDao.loadAll();
		return orderList;
	}


	@Override
	public void createOrder(Orders order) {
		order.setOrderId(orderDao.generateOrderId());
	      orderDao.save(order);
	}


	@Override
	public void removeOrderById(long id) {
		orderDao.deleteByKey(id);
	}


	@Override
	public void modifyOrder(Orders order) {
		
		Orders order1 = orderDao.get(order.getId());
		try {
			BeanUtils.copyProperties(order1, order);
			orderDao.update(order1);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public Orders getOrder(long id){
		Orders order = orderDao.get(id);
		
		return order;
	}
	
	@Override
	public String generateOrderId(){
		String orderId = orderDao.generateOrderId();
		return orderId;
	}

	//add by lcl 2012-3-14
	@Override
	public List<Orders> getOrderListById(long id){
		List<Orders> list = orderDao.find("from Orders where orderId='"+id+"'");
		//List<Orders> list = orderDao.loadAll();
		return list;
	}
	
	public List<Orders> getOrderListById(String id){
		List<Orders> list = orderDao.find("from Orders o where o.id='"+id+"'");
		
		return list;
	}
	
	public void deleteSelectedOrders(String []ids){
		if(ids!=null){  
			Long[] idsl=new Long[ids.length];
			for(int i=0;i<ids.length;i++){
				if(ids[i]!=null||!ids[i].isEmpty()){
					idsl[i]=Long.valueOf(ids[i].trim());
				}
				
			}
			orderDao.deleteSelectedGoods(idsl);
			}
	}
	
	public void updateState(Orders order){
		orderDao.updateState(order);
	}
	
	public void deleteRelateOrder(String orderId){
		orderDao.deleteRelateOrder(orderId);
	}
	
	//add by lcl 2012-09-06
	public List<Orders> getOrdersByUserId(long id){
		return orderDao.getOrdersByUserId(id);
	}


	@Override
	public void createOrder(Orders order, ArrayList<OrderItem> orderDetailList) {
		orderDao.save(order);
		
	}
	
	//orders about cluster
	
    public List<Orders> getClusterCompanyOrdersList(long clusterCompanyid,int offset,int pageSize){
    	
    	return orderDao.getClusterCompanyOrdersList(clusterCompanyid,offset,pageSize);
    }
	
	public List<Orders> getClusterCompanyOrdersList(long clusterCompanyid){
		
		return orderDao.getClusterCompanyOrdersList(clusterCompanyid);
	}
	
    public List<Orders> getOrderByOrderId(String orderId,int offset,int pageSize){
    	
    	return orderDao.getOrderByOrderId(orderId,offset,pageSize);
    }
	
	public List<Orders> getOrderByOrderId(String orderId){
		
		return orderDao.getOrderByOrderId(orderId);
	}
	
    public List<Orders> getOrderByStat(String[] paramValues,int offset,int pageSize){
    	
    	return orderDao.getOrderByStat(paramValues,offset,pageSize);
    }
	
	public List<Orders> getOrderByStat(String[] paramValues){
		
		return orderDao.getOrderByStat(paramValues);
	}
	
    public List<Orders> getOrderByDate(String[] paramValues,int offset,int pageSize){
    	
    	return orderDao.getOrderByDate(paramValues,offset,pageSize);
    }
	
	public List<Orders> getOrderByDate(String[] paramValues){
		
		return orderDao.getOrderByDate(paramValues);
	}
	
	//orders about shengyin
    public List<Orders> getAllOrder(int type,int offset,int pageSize){
    	
    	return orderDao.getAllOrder(type,offset,pageSize);
    }
	
	public List<Orders> getAllOrder(int type){
		
		return orderDao.getAllOrder(type);
	}
	
    public List<Orders> getOrderByStat(String orderStat,int offset,int pageSize){
    	
    	return orderDao.getOrderByStat(orderStat,offset,pageSize);
    }
	
	public List<Orders> getOrderByStat(String orderStat){
		
		return orderDao.getOrderByStat(orderStat);
	}
	
    public List<Orders> getOrderByDate(String date,int offset ,int pageSize){
    	
    	return orderDao.getOrderByDate(date, offset, pageSize);
    }
	
	public List<Orders> getOrderByDate(String date){
		
		return orderDao.getOrderByDate(date);
	}
}
